package cn.com.aiouyi.controller.admin;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.Accessory;
import cn.com.aiouyi.entity.File;
import cn.com.aiouyi.entity.FileClass;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.AccessoryService;
import cn.com.aiouyi.service.FileClassService;
import cn.com.aiouyi.service.FileService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by tommy on 2018/4/26.
 */
@Controller("adminFileController")
@RequestMapping("/admin/file")
public class FileController extends BaseController {

    @Resource(name = "fileServiceImpl")
    private FileService fileService;

    @Resource(name = "fileClassServiceImpl")
    private FileClassService fileClassService;

    @Resource(name = "accessoryServiceImpl")
    private AccessoryService accessoryService;

    @GetMapping("/list")
    public String list(Pageable pageable, ModelMap model) {
        model.put("page", fileService.findPage(pageable));
        return "/admin/file/list";
    }

    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("fileClasses", fileClassService.findAll());
        return "/admin/file/add";
    }

    @PostMapping("/save")
    public String save(File file, Long fileClassId, Long fileId, RedirectAttributes redirectAttributes) {
        if (fileId != null){
            Accessory accessory = accessoryService.find(fileId);
            if (accessory!=null){
                file.setAcc(accessory);
                file.setOriginName(accessory.getOriginName());
            } else {
                addFlashMessage(redirectAttributes, Message.error("文件不能为空"));
                return "redirect:list";
            }
        } else {
            addFlashMessage(redirectAttributes, Message.error("文件不能为空"));
            return "redirect:list";
        }
        if (fileClassId == null) {
            addFlashMessage(redirectAttributes, Message.error("分类不能为空"));
            return "redirect:list";
        }
        FileClass fileClass = fileClassService.find(fileClassId);
        if (fileClass == null) {
            addFlashMessage(redirectAttributes, Message.error("分类不存在"));
            return "redirect:list";
        }
        file.setFileClass(fileClass);
        file.setDownloadCount(0);
        fileService.save(file);
        addFlashMessage(redirectAttributes, Message.SUCCESS);
        return "redirect:list";
    }

    @GetMapping("/edit")
    public String edit(Long id, ModelMap model) {
        model.addAttribute("fileClasses", fileClassService.findAll());
        model.put("file", fileService.find(id));
        return "/admin/file/edit";
    }

    @PostMapping("/update")
    public String update(File file, Long fileClassId, Long fileId, RedirectAttributes redirectAttributes) {
        if (fileId != null){
            Accessory accessory = accessoryService.find(fileId);
            if (accessory!=null){
                file.setAcc(accessory);
                file.setOriginName(accessory.getOriginName());
            } else {
                addFlashMessage(redirectAttributes, Message.error("文件不能为空"));
                return "redirect:list";
            }
        }
        if (fileClassId == null) {
            addFlashMessage(redirectAttributes, Message.error("分类不能为空"));
            return "redirect:list";
        }
        FileClass fileClass = fileClassService.find(fileClassId);
        if (fileClass == null) {
            addFlashMessage(redirectAttributes, Message.error("分类不存在"));
            return "redirect:list";
        }
        file.setFileClass(fileClass);
        fileService.update(file);
        addFlashMessage(redirectAttributes, Message.SUCCESS);
        return "redirect:list";
    }


    @PostMapping("/delete")
    @ResponseBody
    public Message delete(Long[] ids, HttpServletRequest request, Model model){
        if (ids != null && ids.length >0){
            fileService.delete(ids);
            return Message.SUCCESS;
        } else {
            model.addAttribute(ERROR_VIEW_MESSAGE_KEY, "id不能为空");
            return Message.ERROR;
        }
    }
}
